package jdbcmapper;

import lotus.domino.*;

/**
 * <p>Title: JDBC Mapper</p>
 *
 * <p>Description: Libraries to copy documents form Lotus Notes to jdbc
 * table</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: The Salvation ARmy</p>
 *
 * @author Ansel Robateau
 * @version 1.1
 */
public class Main extends AgentRunnerBase{
    private int m_priority;
    private String m_server;
    private String m_database_file;
    public Main() {
        this.m_priority = 0;
    }
    public static void main(String args[]){
        Main main1 = new Main();
        try{
                /*************
                 * -h<host> - The server name
                 * -d<database> - The database file
                 *************/
                System.out.println("Usage:");
                System.out.println("\t-h<host>\t- The server name");
                System.out.println("\t-d<database>\t- The database file name"+
                                  " and path");
                System.out.println("\t-p<priority>\t- The copy priority level"+
                                  " you are selecting.  eg. 0(Instant),1"+
                                  "(Intermediate),2(Hourly),3(Daily),4(Weekly)"+
                                  ",5(Monthly)");
                int count = args.length;
                String database,priority,server;
                server=database=priority = null;
                for(int i = 0; i < count;i++){
                    String arg = args[i];
                    if(arg.startsWith("-h")){
                        server = args[i].substring(2,arg.length());
                    }else if(arg.startsWith("-d")){
                        database = args[i].substring(2,arg.length());
                    }else if(arg.startsWith("-p")){
                        priority = args[i].substring(2,arg.length());
                    }
                }
                if(database == null){
                    System.out.println("No database was specified");
                    return;
                }else{
                    main1.m_server = server;
                    main1.m_database_file = database;
                    if(server == null){
                        System.out.println("No server specified.  Assuming Local");
                    }
                    try{
                        main1.m_priority = Integer.parseInt(priority);
                    }catch(Exception e){
                        System.out.println("No priority specified.  Assuming High priority");
                    }
                }
                main1.start();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    protected Database getDatabase() throws Exception {
        return this.m_session.getDatabase(this.m_server,this.m_database_file);
    }


    protected void execute() throws Exception {
      /*****
       * Open the database and read the documents
       ****/
      DBMapper mapper = new DBMapper(this.m_session,this.getDatabase());
      mapper.setPriority(this.m_priority);
      while(true){
    	  mapper.processMapping(360000,5000);
      }
    }

}
